package com.youi.interceptor;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.entity.Groupleader;
import com.youi.entity.Leaderfan;
import com.youi.entity.Leaderprofit;
import com.youi.entity.Leaderstore;
import com.youi.entity.Sysuser;
import com.youi.entity.User;
import com.youi.entity.Userwechat;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpSessionManage;
import com.youi.service.GroupleaderService;
import com.youi.service.LeaderfanService;
import com.youi.service.LeaderstoreService;
import com.youi.service.UserService;
import com.youi.service.UserwechatService;
import com.youi.third.meiyijiauser.MeyijiauserUtil;
import com.youi.third.meiyijiauser.model.MemberResult;
import com.youi.util.CommonInfoUtil;
import com.youi.util.JedisUtils;
import com.youi.util.JsonUtil;
import com.youi.util.LogUtil;


/**
 * 会话拦截器
 * @author Administrator
 *
 */
@Component
public class SessionInterceptor extends HandlerInterceptorAdapter {
	
	@Autowired
	private UserService userService;
	@Autowired
	private GroupleaderService groupleaderService;
	@Autowired
	private LeaderstoreService leaderstoreService;
	@Autowired
	private UserwechatService userwechatService;
	@Autowired
	private LeaderfanService leaderfanService;
	
	@Override	
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
		String url =request.getRequestURL().toString();
		String token=request.getParameter(SessionKey.TOKEN);		
		LogUtil.Debug("======================");
		LogUtil.Debug("请求url==="+url);
		Map<String, Object> params=null;
//		if (!url.contains("removelock")&&JedisUtils.get("lock")!=null) {
//			return false;
//		}				
		if(url.contains(Common.USERPMS)||url.contains("miniAppPoster")){			
			if (token==null||token.length()==0) {
				throw new BusinessException(CommonInfoUtil.NULLTOKEN, "token参数为空");
			}
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put(Common.TOKEN, token);			
			User user=userService.getByParam(prams);
			if (user!=null) {
				getLoginMsg(user);				
				HttpSessionManage.setAttribute(request, SessionKey.USER, user);
			}else {
				HttpSessionManage.removeAttribute(request, SessionKey.USER);
			}	
			if(HttpSessionManage.getAttribute(request,  SessionKey.USER)==null){//判断用户是否登录
				throw new BusinessException(CommonInfoUtil.NOLOGIN, "用户未登录");
			}
			if (url.contains(Common.LEADER)) { 
				prams=new HashMap<String, Object>();
				prams.put(Common.DELSTATUS, DelStatus.NODEL);
				prams.put(Common.USERID, user.getId()); 
				Groupleader groupleader=groupleaderService.getByParam(prams);
				if (groupleader==null) {
					throw new BusinessException(CommonInfoUtil.NOPERMISSIONS, "非团长无法操作"); } 
				}			 
		}		
		return true;
	}
	
	/**
	  *  拦截器结束请求后执行
	 */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }
    
    private void getLoginMsg(User user) throws BusinessException {
    	Map<String, Object> prams=new HashMap<String, Object>();
		prams.put(Common.DELSTATUS, DelStatus.NODEL);
		prams.put(Common.USERID, user.getId());
		Groupleader groupleader=groupleaderService.getByParam(prams);	
		Userwechat userother=userwechatService.getByParam(prams);
		user.setUnionId(userother.getUnionid());
		if (groupleader!=null) {
			user.setLeaderId(groupleader.getId());
			prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);			
			prams.put("leaderId", groupleader.getId());
			List<Leaderstore>leaderstoreList=leaderstoreService.getByParams(prams);
			for (Leaderstore leaderstore : leaderstoreList) {
				if (leaderstore.getLevel()==1) {
					user.setLeaderLevel(1);						
				}else if (leaderstore.getLevel()==2) {
					user.setLeaderLevel(2);
				}
				break;
			}			
		}else {
			user.setLeaderLevel(0);
		}
		prams=new HashMap<String, Object>();
		prams.put(Common.DELSTATUS, DelStatus.NODEL);	
		LogUtil.Info("unionid="+userother.getUnionid());
		prams.put("unionid", userother.getUnionid());
		Leaderfan leaderfan=leaderfanService.getByParam(prams);
		LogUtil.Info("leaderfan="+leaderfan);
		if (leaderfan==null) {
			LogUtil.Info("验证是否粉丝");
			user.setIsFans(0);
		}else {
			user.setIsFans(1);
		}
    }
    
    public static void main(String[] args) {
		String url="http://127.0.0.1:8088/Nailart/storeuserGetJsonInfo/storeUserGetUser.do?openid=3";
		System.err.println(url.contains(Common.USERPMS));
	}
}
